<?php

require_once BaseUtil::getModulePath("base") . '/controllers/BaseController.php';
require_once BaseUtil::getModulePath("config") . '/services/ConfigService.php';
require_once BaseUtil::getModulePath("config") . '/forms/ConfigForm.php';

class Config_ConfigController extends BaseController {

    public function init() {
        parent::init();
    }

    public function indexAction() {
        $configService=new ConfigService();
        $configForm=new ConfigForm(true);
        $config=$configService->getConfigByKey();
        $configForm->getElement("verification_email")->setValue($config["verification_email"]);
        $configForm->getElement("template_name")->setValue($config["template_name"]);
        $configForm->getElement("shipping_rate_1")->setValue($config["shipping_rate_1"]);
        $configForm->getElement("shipping_rate_2")->setValue($config["shipping_rate_2"]);
        $configForm->getElement("shipping_rate_3")->setValue($config["shipping_rate_3"]);
        $configForm->getElement("shipping_rate_4")->setValue($config["shipping_rate_4"]);
        $configForm->getElement("shipping_rate_5")->setValue($config["shipping_rate_5"]);
        $this->view->form = $configForm;
    }

    public function addAction() {
        $configForm = new ConfigForm();
        $this->view->form = $configForm;
    }

    public function subaddAction() {
        $configForm = new ConfigForm();
        $configService = new ConfigService();
        if (!$configForm->isValid ($_POST)) {
            $this->view->form = $configForm;
            return $this->render('add');
        }
        $configService->addConfig($configForm);
        $this->_redirect('/config/config');
    }

    public function subeditAction() {

        $configForm = new ConfigForm(true);
        $configService = new ConfigService();
        if(!$configForm->isValid($_POST)) {
            $this->view->form = $configForm;
            return $this->render('index');
        }
        $configService->editConfig($configForm);
        $this->_redirect('/config/config');
    }

    public function deletesomeAction() {
        $idsStr=HttpUtil::getPostValue("selectIds");
        $ids=explode(",",$idsStr);
        $configService=new ConfigService();
        $configService->deleteConfigs($ids);
        $this->_redirector->gotoUrl('/config/config');
    }
}